package yunhe.util;

import org.apache.ibatis.session.SqlSession;
import yunhe.dao.IcreatSeq;
import yunhe.entity.AutoId;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.sql.Date;


/**
 * 通用工具类
 */
public class GeneralUtil {

	/**
	 * 输入字符串转化为32位的MD5字符串返回
	 * @param str
	 * @return
	 */
	public static String str2md5(String str) {
		String s = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(str.getBytes("UTF-8"));
			byte[] m = md5.digest();
			StringBuilder sb = new StringBuilder();
			for (byte b : m) {
				int bb = Byte.toUnsignedInt(b);

				if (bb <= 0xf) {
					sb.append(0);
				}
				sb.append(Integer.toHexString(bb));
			}
			s = sb.toString();
		} catch (Exception e) {
			s = null;
		}

		return s;
	}

	/**
	 * 根据name获取value
	 * 
	 * @param cookies
	 * @param name
	 * @return
	 */

	public static String getValue(Cookie[] cookies, String name) {

		if (cookies != null && cookies.length > 0) {
			for (Cookie c : cookies) {
				if (c.getName().equals(name)) {
					return c.getValue();
				}
			}
		}

		return "";
	}

	/**
	 * 新加cookie
	 * 
	 * @param response
	 * @param cookies
	 * @param maxAge
	 */
	public static void addCookies(HttpServletResponse response, Cookie[] cookies, int maxAge) {
		for (Cookie c : cookies) {
			c.setMaxAge(maxAge);
			response.addCookie(c);
		}
	}

	/**
	 * 字符串转为util.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.util.Date stringToUtilDate(String date, String format) {
		java.util.Date time = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			time = sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return time;
	}

	/**
	 * 字符串转为sql.Date
	 * 
	 * @param date
	 * @return
	 */
	public static java.sql.Date stringToSqlDate(String date, String format) {
		java.util.Date utime = GeneralUtil.stringToUtilDate(date, format);
		java.sql.Date stime = new java.sql.Date(utime.getTime());
		return stime;
	}
	/**
	 * 返回本地制定的序列值
	 * @param header 字母头
	 * @return 返回 字母+日期+序号  例如ex201706020001
	 */
	private static String synchronizedStr="syn";//用于同步线程
	public static String creatSeq(String header){
		SqlSession ssi=SqlSessionFactoryUtil.getSqlSession();
		IcreatSeq is=ssi.getMapper(IcreatSeq.class);
		String s;
		synchronized (synchronizedStr) {
			AutoId ai = is.search();
			String sqlTime=changeDate2Str(ai.getTime(),"yyyyMMdd");
			String newTime=changeDate2Str(new java.util.Date(),"yyyyMMdd");
			/*与当前是同日期*/
			if (sqlTime.equals(newTime)){
				//System.out.println(Long.parseLong(sqlTime)*10000);
				s=header+((Long.parseLong(sqlTime)*10000)+(ai.getAutoId()+1));
				ai.setAutoId(ai.getAutoId()+1);
			}
			else{//如果不是当前日，把当前日更新后写入
				s=header+newTime+"0001";
				ai.setAutoId(1);
				ai.setTime(new Date(new java.util.Date().getTime()));
			}
			is.update(ai);
			ssi.commit();
		}
		return s;
	}

	/**
	 * 将日期转换成制定格式字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public  static String changeDate2Str(java.util.Date date,String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.format(date);
	}
	public static int getTotalPages(int pageSize,int count){
		int totalPages=(count%pageSize==0?count/pageSize:count/pageSize+1);
		return totalPages;
	}
}
